package com.cykj.service.impl;

import com.cykj.mapper.PatientMapper;
import com.cykj.pojo.CyPatient;
import com.cykj.service.PatientService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Override
    public ResponseDto getAllPatient(int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyPatient> allPatient = patientMapper.getAllPatient();
        if (allPatient != null) {
            PageInfo<Object> pageInfo = new PageInfo<>(page);
            PageResult pageResult = PageUtils.getPageResult(pageInfo);
            return new ResponseDto(1, "查询成功", pageResult);
        } else {
            return new ResponseDto(0, "查询失败", null);
        }

    }

    @Override
    public ResponseDto deletePatient(int patientId) {
        int i = patientMapper.deletePatient(patientId);
        if (i != 0){
            return new ResponseDto(1,"删除成功",null);
        }else {
            return new ResponseDto(0,"删除失败",0);
        }
    }

    @Override
    public ResponseDto alterInfo(int patientId, double patientMoney, int patientHeight, int patientWeight) {
        int i = patientMapper.selectIsAlter(patientId, patientMoney, patientHeight, patientWeight);
        if (i !=0){
            //说明暂未修改
            return new ResponseDto(2,"暂未修改",null);
        }
        //说明值有被修改
        int i1 = patientMapper.alterInfo(patientId, patientMoney, patientHeight, patientWeight);
        return new ResponseDto(1,"修改成功",null);
    }

    @Override
    public ResponseDto selectByData(int sort, String value, int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyPatient> cyPatients = patientMapper.selectByData(sort, value);
        if (cyPatients != null) {
            PageInfo<Object> pageInfo = new PageInfo<>(page);
            PageResult pageResult = PageUtils.getPageResult(pageInfo);
            return new ResponseDto(1, "查询成功", pageResult);
        } else {
            return new ResponseDto(0, "查询失败", null);
        }
    }

    @Override
    public ResponseDto deleteUserByIds(List<Integer> ids) {
        int i = patientMapper.deleteUserByIds(ids);
        if (i > 0){
            return new ResponseDto(1,"操作成功",i);
        }else {
            return new ResponseDto(0,"操作失败",null);
        }
    }

    @Override
    public ResponseDto addCommit(String name, int age, String phone, String cardID, int height, int weight, String radio,int money) {
        System.out.println(money);
        int count = patientMapper.selectAddIsExist(name, cardID);
        if (count == 1){
            //说明该用户已经存在
            return new ResponseDto(2,"该用户已经注册",null);
        }
        //执行到这说明该添加用户信息
        if (radio == "1"){
            radio = "男";
        }else{
            radio = "女";
        }
        int i = patientMapper.addCommit(name, age, phone, cardID, height, weight, radio,money);
        return new ResponseDto(1,"添加成功",null);
    }

    @Override
    public ResponseDto selectAgeCount(List<Integer> ages) {
        List<Integer> countAge = new ArrayList<>();
        countAge.add(0);
        for (int i = 0; i < ages.size()-1; i++) {
            int age1 = ages.get(i);
            int age2 = ages.get(i+1);
            int count = patientMapper.selectAgeCount(age1, age2);
            countAge.add(count);
        }

        return new ResponseDto(1,"年龄段查询成功",countAge);

    }

    @Override
    public ResponseDto selectMoneyCount(String age, String time1, String time2) {
        //age 0~10,11~24,25~40,41~60,61~80,81~100
        System.out.println(age);
        String[] split = age.split(",");
        List<Integer> money = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            System.out.println("-===================");
            System.out.println(split.length);
            String[] split1 = split[i].split("~");
            int age1 = Integer.parseInt(split1[0]);
            int age2 = Integer.parseInt(split1[1]);
            System.out.println(age1 +"------"+age2);
            String moneyStr = patientMapper.selectMoneyCount(age1, age2, time1, time2);
            double moneyDouble = Double.parseDouble(moneyStr);
            int moneyCount = (int) Math.round(moneyDouble); // 使用 Math.round() 进行四舍五入
            money.add(moneyCount);
        }
        return new ResponseDto(1,"年龄段花费金额查询成功",money);
    }

    //public int selectCount() {
    //    return patientMapper.selectCount();
    //}
    //public List<CyPatient> allPatient(){
    //    return patientMapper.getAllPatient();
    //}

}
